﻿Imports System.ComponentModel

Public Class ViewModelBase
    Implements INotifyPropertyChanged

#Region " Declarations "

    Private _objUIValidationErrorDictionary As New Dictionary(Of String, UIValidationError)

#End Region

#Region " Events "

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

#End Region

#Region " Properties "

    Public ReadOnly Property UIValidationErrorCount() As Integer
        Get
            Return _objUIValidationErrorDictionary.Count
        End Get
    End Property

    Public ReadOnly Property UIValidationErrorMessages() As String
        Get

            If UIValidationErrorCount > 0 Then

                Dim sb As New System.Text.StringBuilder(1024)

                For Each kvp As KeyValuePair(Of String, UIValidationError) In _objUIValidationErrorDictionary
                    sb.AppendLine(kvp.Value.ToFriendlyErrorMessage)
                Next

                Return sb.ToString

            Else
                Return String.Empty
            End If

        End Get
    End Property

#End Region

#Region " Constructor "

    Public Sub New()
    End Sub

#End Region

#Region " PropertyChanged Methods "

    Protected Sub OnPropertyChanged(ByVal strPropertyName As String)

        If Me.PropertyChangedEvent IsNot Nothing Then
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(strPropertyName))
        End If

    End Sub

#End Region

#Region " UIValidationError Methods "

    Public Sub AddUIValidationError(ByVal e As UIValidationError)
        _objUIValidationErrorDictionary.Add(e.Key, e)
        OnPropertyChanged("UIValidationErrorMessages")
        OnPropertyChanged("UIValidationErrorCount")
    End Sub

    Public Sub RemoveUIValidationError(ByVal e As UIValidationError)
        _objUIValidationErrorDictionary.Remove(e.Key)
        OnPropertyChanged("UIValidationErrorMessages")
        OnPropertyChanged("UIValidationErrorCount")
    End Sub

    Protected Sub ClearUIValidationErrors()
        _objUIValidationErrorDictionary.Clear()
        OnPropertyChanged("UIValidationErrorMessages")
        OnPropertyChanged("UIValidationErrorCount")
    End Sub

#End Region


End Class
